const http = require('http')
const url = require('url')
const fs = require('fs')
const path = require('path')
const stu = require('./students')
const ejs = require('ejs')

const server = http.createServer((req,res)=>{
    console.log(req.url);
    let urlObj = url.parse(req.url,true)
    
    if(req.url ==='/'){
        res.setHeader('Content-Type','text/html;charset=utf8')
        let filePath = path.join(__dirname,'./public/index.html')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            // res.end(data)
            stu.findAll((err,students)=>{
                if(err){
                    throw err
                }
                
                let content = ``
                students.forEach((item,index)=>{
                    content+=`
                    <tr>
                    <td>${index}</td>
                    <td>${item.name}</td>
                    <td>${item.age}</td>
                    <td>${item.gender}</td>
                    <td>${item.hobbies}</td> 
                    <td>
                    <a href="/update?id=${item.id}">修改</a>
                    <a href="/delete?id=${item.id}">删除</a>
                    </td> 
                    </tr>
                    `
                })
                data = data.replace('=_=',content)
                res.end(data)
            })
        }) 
    }else if(req.url ==='/node_modules/bootstrap/dist/css/bootstrap.min.css'){
        res.setHeader('Content-Type','text/css')
        let filePath = path.join(__dirname,'/node_modules/bootstrap/dist/css/bootstrap.min.css')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.end(data)
        })
    }else if(req.url==='/node_modules/jquery/dist/jquery.min.js'){
        res.setHeader('Content-Type','application/x-javascript')
        let filePath = path.join(__dirname,'/node_modules/jquery/dist/jquery.min.js')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.end(data)
        })
        
    }else if(req.url==='/node_modules/echarts/dist/echarts.js'){
        res.setHeader('Content-Type','application/x-javascript')
        let filePath = path.join(__dirname,'/node_modules/echarts/dist/echarts.js')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.end(data)
        })
        
    }
    else if(req.url === '/public/js/index.js'){
        res.setHeader('Content-Type','application/x-javascript')
        let filePath = path.join(__dirname,'/public/js/index.js')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.end(data)
        })
    }
    else if(req.url ==='/node_modules/bootstrap/dist/js/bootstrap.min.js'){
        res.setHeader('Content-Type','application/x-javascript')
        let filePath = path.join(__dirname,'/node_modules/bootstrap/dist/js/bootstrap.min.js')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.end(data)
        })
    }else if(req.url ==='/new.html'){
        res.setHeader('Content-Type','text/html;charset=utf8')
        let filePath = path.join(__dirname,'/public/new.html')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.end(data)
        })
    }else if(urlObj.pathname === '/addStudent'){
        
        let student = urlObj.query
       
        stu.addStudent(JSON.stringify(student),(err)=>{
            if(err){
                throw err
            }
            res.statusCode=302
            res.setHeader('Location','/')
            res.end()
        })
        
        
    }else if(urlObj.pathname === '/delete'){
        let id =  urlObj.query.id
        stu.deleteStudentById(Number(id),(err)=>{
            if(err){
                throw err
            }

            res.statusCode=302
            res.setHeader('Location','/')
            res.end()
        })
    }else if(urlObj.pathname === '/update'){
        res.setHeader('Content-Type','text/html;charset=utf8')
        let filePath = path.join(__dirname,'/public/update.html')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            let id = urlObj.query.id
            stu.findStudentById(Number(id),(err,s)=>{
                if(err){
                    throw err
                }
               
                let str = `
                <form class="form-horizontal" action="/updateS" method="GET">
                <input type="hidden" class="form-control" name="id"  value = ${s.id}>
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">姓名：</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="name" id="name" value = ${s.name}>
                    </div>
                </div>
                <div class="form-group">
                    <label for="age" class="col-sm-2 control-label" >年龄：</label>
                    <div class="col-sm-10">
                        <input type="number" class="form-control" name="age" id="age" value = ${s.age}>
                    </div>
                </div>
    
                <div class="form-group">
                    <label for="gender" class="col-sm-2 control-label">性别：</label>
                    <div class="col-sm-10">
                        <input type="radio" class="radio-line" name="gender" id="gender" value=1>男
                        <input type="radio" class="radio-line" name="gender" id="gender" value=0>女
                    </div>
                </div>
                <div class="form-group">
                    <label for="hobbies" class="col-sm-2 control-label">爱好：</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="hobbies" id="hobbies" value=${s.hobbies}>
                    </div>
                </div>
      
                <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">修改</button>
                </div>
                </div>
        </form>
                `
                data=data.replace('=_=',str)
                res.end(data)
            })
           
        })
    }else if(urlObj.pathname === '/updateS'){
        let s = urlObj.query
        console.log('111');
        stu.updateStudent(JSON.stringify(s),(err)=>{
            if(err){
                throw err
            }
            res.statusCode=302
            res.setHeader('Location','/')
            res.end()

        })
    }

    else if(req.url === '/css/main.css'){
        res.setHeader('Content-Type','text/css')
        let filePath = path.join(__dirname,'/public/css/main.css')
        fs.readFile(filePath,'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.end(data)
        })
    }
})

server.listen(3000,()=>{
    console.log(`server is running...`);
})